package com.wmada.spring.validate;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author will.ma
 * @date 2020-9-28
 */
public class PasswordValidator implements ConstraintValidator<ValidPassword, String> {

    private static final String REGEX = "^[a-zA-Z0-9!@#$%^&*]{8,16}$";

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if (s == null) {
            return true;
        }
        int length = s.trim().length();
        if (length < 8 || length > 16) {
            return false;
        }
        return s.matches(REGEX);
    }
}
